package com.dfdchem.imd.kaiwu.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dfdchem.imd.kaiwu.R;

public class CustomDialog extends Dialog {
    private String title;
    private String content;
    private String buttonConfirm;
    private String buttonCancel;
    private View.OnClickListener confirmClickListener;
    private View.OnClickListener cancelClickListener;
    private static final int SHOW_ONE = 1;
    private int show = 2;

    public CustomDialog(Context context,
                        String title,
                        String content,
                        String buttonConfirm,
                        View.OnClickListener confirmClickListener) {
        super(context, R.style.CustomDialog);
        this.title = title;
        this.content = content;
        this.buttonConfirm = buttonConfirm;
        this.confirmClickListener = confirmClickListener;
        this.show = SHOW_ONE;
    }
    public CustomDialog(Context context,
                        String title,
                        String content,
                        String buttonConfirm,
                        View.OnClickListener confirmClickListener,
                        String buttonCancel) {
        super(context, R.style.CustomDialog);
        this.title = title;
        this.content = content;
        this.buttonConfirm = buttonConfirm;
        this.buttonCancel = buttonCancel;
        this.confirmClickListener = confirmClickListener;
    }

    public CustomDialog(Context context,
                        String title,
                        String content,
                        String buttonConfirm,
                        View.OnClickListener confirmClickListener,
                        String buttonCancel,
                        View.OnClickListener cancelClickListener) {
        super(context, R.style.CustomDialog);
        this.title = title;
        this.content = content;
        this.buttonConfirm = buttonConfirm;
        this.buttonCancel = buttonCancel;
        this.confirmClickListener = confirmClickListener;
        this.cancelClickListener = cancelClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog_layout);
        TextView dialog_title = (TextView) findViewById(R.id.dialog_title);
        TextView dialog_content = (TextView) findViewById(R.id.dialog_content);
        Button dialog_confirm = (Button) findViewById(R.id.dialog_confirm);
        Button dialog_cancel = (Button) findViewById(R.id.dialog_cancel);
        View dialog_line = findViewById(R.id.dialog_line);
        if (!TextUtils.isEmpty(title))
            dialog_title.setText(title);
        if (!TextUtils.isEmpty(content)) {
            dialog_content.setText(content);
            dialog_content.setMovementMethod(ScrollingMovementMethod.getInstance());
        } else {
            dialog_content.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(buttonConfirm))
            dialog_confirm.setText(buttonConfirm);
        if (!TextUtils.isEmpty(buttonCancel))
            dialog_cancel.setText(buttonCancel);
        if (SHOW_ONE == show) {
            dialog_line.setVisibility(View.GONE);
            dialog_cancel.setVisibility(View.GONE);
            if (null != confirmClickListener) {
                dialog_confirm.setOnClickListener(confirmClickListener);
            }
            dialog_confirm.setBackgroundResource(R.drawable.custom_dialog_text_selector_left);
        } else {
            if (null != confirmClickListener) {
                dialog_confirm.setOnClickListener(confirmClickListener);
            } else {
                dialog_confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CustomDialog.this.dismiss();
                    }
                });
            }
            if (null != cancelClickListener) {
                dialog_cancel.setOnClickListener(cancelClickListener);
            } else {
                dialog_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CustomDialog.this.dismiss();
                    }
                });
            }

        }
    }

    public void setCannotBackPress() {
        this.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                    return true;
                }
                return false;
            }
        });
    }
}
